package com.huli.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huli.domain.Carport;
import com.huli.domain.Garage;
import com.huli.exception.CustomException;
import com.huli.mapper.CarportMapper;
import com.huli.mapper.GarageMapper;
import com.huli.param.CarportParam;
import com.huli.service.CarportService;
import com.huli.vo.CarportVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class CarportServiceImpl extends ServiceImpl<CarportMapper, Carport> implements CarportService {
    @Autowired
    CarportMapper carportMapper;

    @Autowired
    GarageMapper garageMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 更改指定车位的状态
     *
     * @param carportParam
     * @return
     */
    @Override
    public Carport updateCarportStatusByCarportId(CarportParam carportParam) {
        Carport carport = new Carport();
        BeanUtils.copyProperties(carportParam, carport);
        carport = carportMapper.selectById(carport.getCarportId());
        if (carportMapper.updateById(carport) > 0) {
            carport.setCarportStatus(carportParam.getCarportStatus());
            return carport;
        } else {
            return null;
        }
    }

    /**
     * 查询所有车位,不传值就是查所有
     * 可以通过传入一个车库的Id号查询,车库的车位
     *
     * @param carportParam
     * @return
     */
    @Override
    public IPage<CarportVo> selectAllCarports(CarportParam carportParam) {
        Page<CarportVo> page = new Page<>(carportParam.getPageIndex(), carportParam.getPageNum());
        System.out.println(carportParam.getGarageId());
        QueryWrapper<CarportVo> queryWrapper = new QueryWrapper<>();
        if (carportParam.getGarageId() != null && carportParam.getGarageId() > 0)
            queryWrapper.eq("g.garage_id", carportParam.getGarageId());
        return carportMapper.selectAllCarport(page, queryWrapper);
    }

    @Override
    public Garage selectGarageByCarportId(Integer garageId) {
        return garageMapper.selectById(garageId);
    }


    @Override
    public IPage<Garage> selectNearGarage(CarportParam carportParam) {

        if (!StringUtils.isNotEmpty(carportParam.getLatitudeAndLongitude())) throw new CustomException("坐标位空");

        String longitude = null;
        String latitude = null;
        String[] split = carportParam.getLatitudeAndLongitude().split(",");
        longitude = split[0];
        latitude = split[1];
        Double aDouble = Convert.convert(double.class, longitude);
        Double aDouble1 = Convert.convert(double.class, latitude);
        GeoResults<RedisGeoCommands.GeoLocation<String>> location = stringRedisTemplate.opsForGeo().radius(
                "garage",//名字
                new Circle(new Point(aDouble, aDouble1),//用户位置
                carportParam.getDistance()),//查询范围
                RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending());//排序方式
        assert location != null;
        Iterator<GeoResult<RedisGeoCommands.GeoLocation<String>>> iterator = location.getContent().iterator();

        List<String> garageIds = new ArrayList<>();
        while (iterator.hasNext()) {
            garageIds.add(iterator.next().getContent().getName());
        }

        QueryWrapper<Garage> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("garage_id", garageIds);

        garageIds = null;
        return garageMapper.selectPage(new Page<Garage>(carportParam.getPageIndex(), carportParam.getPageNum()), queryWrapper);
    }
}
